<template>
	<custom-page title="编辑资料" borderBottomColor="transparent" backgroundColor="#F9F9FB">
		<template #content>
			<view class="pesonal-info-layout">
				<view class="personal-photo-view">
					<custom-choose-media @selectBack="selectImage">
						<template #content>
							<view class="personal-photo-center">
								<image class="personal-photo-img" :src="getAvator(playerInfo.sculptureUrl)" mode="aspectFill"></image>
								<view class="camera">
									<image :src="asserts.ic_camera"></image>
								</view>
							</view>
						</template>
					</custom-choose-media>
				</view>
				<view class="item-row" @click="toEditClick('nick')">
					<view class="title">昵称</view>
					<view class="content">{{playerInfo.name || '请输入昵称'}}</view>
					<image class="right-img" :src="asserts.ic_arrow_right"></image>
				</view>
				<view class="divider"></view>
				<view class="item-row" @click="toEditClick('sex')">
					<view class="title">性别</view>
					<view class="content">{{playerInfo.sexDesc || '请选择性别'}}</view>
					<image class="right-img" :src="asserts.ic_arrow_right"></image>
				</view>
				<view class="divider"></view>
				<view class="item-row" @click="toEditClick('birthday')">
					<view class="title">生日</view>
					<view class="content">{{playerInfo.birthdayTime || '请选择出生年月'}}</view>
					<image class="right-img" :src="asserts.ic_arrow_right"></image>
				</view>
			</view>
		</template>
	</custom-page>
</template>
<script>
	import {
		apis
	} from '../../global/config.js'
	export default {
		data() {
			return {
				playerInfo: {},
			}
		},
		computed: {
			getAvator(){
				return (avator) => {
					if(avator){
						return avator
					}else{
						return this.asserts.ic_avatar_default
					}
				}
			},
		},
		onLoad() {
			this.getPlayerInfo()
		},
		methods: {
			//用户详情（根据Token获取）
			getPlayerInfo(){
				this.requestCenter.getPlayerInfo().then(res => {
					this.playerInfo = res
				})
			},
			
			//图片选择
			selectImage(data){
				let avatarUrl = data.data
				let uploadParams = {
					operateType: 1,		//oss操作类型枚举 1-用户 2-商品 3-展现	
					ossStorageType: 1,		//oss存储类型枚举 1-图片 2-文本 3-视频	
				}
				// #ifdef H5 || APP-PLUS
				uni.showLoading({
					title:'加载中'
				})
				uni.uploadFile({
					url: apis.uploadFile, //上传文件接口地址
					filePath: avatarUrl,
					name: 'file',
					formData: uploadParams,
					success: (uploadFileRes) => {
						uni.hideLoading()
						if(uploadFileRes.statusCode == '200'){
							let uploadFileResFor = JSON.parse(uploadFileRes.data)
							
							let params = {
								id: this.playerInfo.id,
								sculptureUrl: uploadFileResFor.data.fileUrl
							}
							this.requestCenter.updatePlayerInfo(params).then(res => {
								this.getPlayerInfo()
							})
						}
					}
				});
				// #endif
				// #ifdef MP
				this.requestCenter.uploadFile(avatarUrl, uploadParams).then(upload => {
					if (upload) {
						let params = {
							id: this.playerInfo.id,
							sculptureUrl: upload.fileUrl
						}
						this.requestCenter.updatePlayerInfo(params).then(res => {
							this.getPlayerInfo()
						})
					}
				})
				// #endif
			},
			
			//点击编辑信息
			toEditClick(type){
				uni.navigateTo({
					url: `/pages/assistant-personal-info-edit/assistant-personal-info-edit?type=${type}&userType=1`,
					events: {
						personalEdit: (data)=> {
							this.getPlayerInfo()
						}
					}
				})
			},
		}
	}
</script>

<style scoped lang="less">
	.pesonal-info-layout{
		width: 100%;
		display: flex;
		flex-direction: column;
		background-color: white;
		padding: 64rpx 32rpx 0 32rpx;
		box-sizing: border-box;
		.personal-photo-view{
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: center;
			margin-bottom: 64rpx;
			.personal-photo-center{
				width: 200rpx;
				height: 200rpx;
				position: relative;
				.personal-photo-img{
					width: 200rpx;
					height: 200rpx;
					border-radius: 50%;
				}
				.camera{
					position: absolute;
					width: 64rpx;
					height: 64rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 50%;
					bottom: 10rpx;
					right: -6rpx;
					background-color: #021624;
					image{
						width: 32rpx;
						height: 32rpx;
					}
				}
			}
		}
		.divider{
			width: 100%;
			height: 1rpx;
			background-color: #F5F5F5;
		}
		.item-row{
			width: 100%;
			height: 100rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			.title{
				font-size: 28rpx;
				color: #585F70;
				flex: 1;
			}
			.content{
				font-size: 28rpx;
				color: #021624;
				margin-right: 20rpx;
			}
			image{
				width: 20rpx;
				height: 21rpx;
			}
		}
	}
</style>
